package wxk.study.springtx.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import wxk.study.springtx.dal.dao.TGroupMapper;
import wxk.study.springtx.dal.dao.TUserMapper;
import wxk.study.springtx.dal.model.TGroup;
import wxk.study.springtx.service.GroupService;
import wxk.study.springtx.service.UserService;


import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/17.
 * @since 2.2
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {
    private static final Logger LOGGER = Logger.getLogger(GroupServiceImpl.class);

    @Autowired
    private TGroupMapper tGroupMapper;

    @Resource(name = "userService")
    private UserService userService;

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int addGroup(TGroup group) {
        LOGGER.info("开始添加组");
        int result = tGroupMapper.insert(group);
        LOGGER.info("添加组完成");
        return result;
    }

    public int updateGroup(TGroup group) {
        return 0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public int addMembers(TGroup group, String... nickNames) {
        this.addGroup(group);
        userService.batchJoinGroup(group.getgCode(), nickNames);
        int i = 1 / 0;
        return 0;
    }
}
